package chap10collection.test5_6_13;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * @author zdl
 * @date 2024/12/17
 */
class Worker {
    private int age;
    private String name;
    private double salary;
    public Worker (){}
    public Worker (String name, int age, double salary){
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getSalary(){
        return salary;
    }
    public void setSalary(double salary){
        this.salary = salary;
    }
    public void work(){
        System.out.println(name + "work20241228");
    }
    //重写equals方法  用于比较对象存储的属性值是否相同，若相同返回ture
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Worker worker = (Worker) o;
        return age == worker.age && Double.compare(salary, worker.salary) == 0 && Objects.equals(name, worker.name);
    }

    @Override
    public int hashCode() {
        //1.默认生成
        //return Objects.hash(age, name, salary);
        //2. return 0;
        //3.
        /*int result = 0;
        if (name != null) result = name.hashCode();
        return result + age;*/

        //4.
        return super.hashCode();
    }
}
public class Demo5_6_13 {
    public static void main(String[] args) {
        Worker zhangsan3 = new Worker("zhangsan3", 18, 3000);
        Worker li4 = new Worker("li4", 25, 3500);
        Worker wang5 = new Worker("wang5", 22, 3200);
        //1) 创建一个 List，在 List 中增加三个工人
        List<Worker> list = new ArrayList<>();
        list.add(zhangsan3);
        list.add(li4);
        list.add(wang5);
        for (Worker worker : list) {
            System.out.print(worker.getName() + "\t" + worker.getAge() + "\t" + worker.getSalary() + "\n");
        }
        System.out.println("===============================");
        // 2)在 li4 之前插入一个工人，信息为：姓名：zhao6，年龄：24，工资 3300
        Worker zhao6 = new Worker("zhao6", 24, 3300);
        int i = list.indexOf(li4);
        list.add(i , zhao6);
        for (Worker worker : list) {
            System.out.print(worker.getName() + "\t" + worker.getAge() + "\t" + worker.getSalary() + "\n");
        }
        //3)删除 wang5 的信息
        list.remove(list.indexOf(wang5));
        System.out.println("===============================");
        for (Worker worker : list) {
            System.out.print(worker.getName() + "\t" + worker.getAge() + "\t" + worker.getSalary() + "\n");
        }
        System.out.println("===============================");
        //4) 利用 for 循环遍历，打印 List 中所有工人的信息
        //for循环遍历
        for (int j = 0; j < list.size(); j++) {
            System.out.println(list.get(j).getName() + "\t" + list.get(j).getAge() + "\t" + list.get(j).getSalary());
        }
        System.out.println("===============================");
        //增强for循环
        for (Worker worker : list) {
            System.out.print(worker.getName() + "\t" + worker.getAge() + "\t" + worker.getSalary() + "\n");
        }
        System.out.println("===============================");
        //5) 利用迭代遍历，对 List 中所有的工人调用 work 方法。
        Iterator<Worker> iterator = list.iterator();
        while (iterator.hasNext()) {
            iterator.next().work();
        }

        //6) 为 Worker 类添加 equals 方法
        Worker w1 = new Worker("jack", 22, 3200);
        Worker w2 = new Worker("jack", 22, 3200);
        System.out.println(w1.equals(w2));  //true
    }
}
